<?php
/**
 * Created by PhpStorm.
 * User: dale
 * Date: 2017/10/27
 * Time: 下午3:52
 */

namespace Admin\Mobile\Controllers;


use Admin\Frontend\Controllers\UserController;
use Admin\Models\sys\XhsysUserSetting;
use Admin\Models\user\XhsysUser;
use Helpers\Assets;
use Helpers\Main;
use Helpers\User;

class MineController extends ControllerBase
{
    /**
     * 覆盖父类方法
     *
     * @param $msg
     * @param bool $praseLog
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale 20171219
     */
    public function sendSuccessResponse($msg, $praseLog = true)
    {
        $praseLog and parent::praseLog($msg,$msg);
        return $this->response->setJsonContent(['code' => 200, 'msg' => $msg ?: '成功！'], JSON_UNESCAPED_UNICODE);
    }

    /**
     * 覆盖父类方法
     *
     * @param $code
     * @param $msg
     * @param bool $praseLog
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale 20171219
     */
    public function sendErrorResponse($code, $msg, $praseLog = true)
    {
        $praseLog and parent::praseLog($msg,$msg);
        return $this->response->setJsonContent(['code' => $code, 'error' => $msg ?: '错误！'], JSON_UNESCAPED_UNICODE);
    }

    /**
     * 修改账号接口
     *
     * create by dale 20171219
     */
    public function changePwdAction(){
        Assets::easyUIMobile();
        $this->view->pick('mine/changePwd');
        Assets::importJs('js/myTool.js');
        Assets::importJs('js/mine/changePwd.js');
    }

    /**
     * 保存修改
     *
     * create by dale 20171219
     */
    public function saveChangeAction(){
        $oldPwd  = $this->request->getPost('oldPwd','string');
        $pwd     = $this->request->getPost('pwd','string');
        $confirm = $this->request->getPost('confirm','string');
        if (!$oldPwd || !$pwd || !$confirm){
           return $this->sendErrorResponse(400, '缺少参数');
        }
        $userId = Main::getLoginUserInfo('userId');
        $check = XhsysUser::findFirst([
            'conditions' => 'bDelete = 0 AND iUserId = :iUserId: AND sLogonPwd = :sLogonPwd:',
            'bind'       => ['iUserId' => $userId, 'sLogonPwd' => md5($oldPwd) ],
        ]);
        if (!$check){
            return $this->sendErrorResponse('400', '原密码不正确，请检查');
        }
        if ($pwd != $confirm){
            return $this->sendErrorResponse('400', '两次输入密码不一致');
        }
        try{
            $res = User::setPassword($userId,$pwd);
            if($res){
                return $this->sendSuccessResponse('修改成功');
            }
        }catch (\Exception $e){
            return $this->sendErrorResponse(500,$e->getMessage());
        }
    }



    public function destoryCookieAction(){
        if ($this->cookies->has("password")){
            $this->cookies->set("password",'',time() - 7 * 86400);
        }
    }

    /**
     * 信任设备
     *
     * create by dale  20180809
     */
    public function trustDevListAction(){
        Assets::easyUIMobile();
        $this->view->pick('tp/listView');
        Assets::importJs('js/mine/trustDevList.js');
    }

    /**
     * 初始化信任设备列表
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by dale  20180810
     */
    public function initTrustListAction(){
        try {
            $userId = Main::getLoginUserInfo('userId');
            $objArr = XhsysUserSetting::find([
                'conditions' => 'bDelete = 0 and userId = :userId: and target =:target: ',
                'bind'       => ['userId' => $userId, 'target' => 'MOBILE_UUID']
            ])->toArray();
            foreach ($objArr as &$obj) {
                $obj['extra'] = $obj['extra'] ? unserialize($obj['extra']) : [];
            }
            unset($obj);
            return $this->sendJsonResponse($objArr);
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage(), $e instanceof XhException ? $e->getDescription() : $e->getTrace());
        }
    }


    public function delTrustAction(){
        try {
            $id = $this->request->getPost('id','int');
            if (!$id) {
                return $this->sendErrorResponse(400,'缺少参数');
            }
            $obj =  XhsysUserSetting::findFirst($id);
            if (!$obj->delete()) {
                return $this->sendErrorResponse(500, '删除失败');
            }
            return $this->sendSuccessResponse('删除成功');
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage(), $e instanceof XhException ? $e->getDescription() : $e->getTrace());
        }
    }



}